package com.guanglan.controller;

import com.guanglan.entity.Menu;
import com.guanglan.entity.SysPermission;
import com.guanglan.service.ShiroService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 作者: 光蓝Comet
 * Date: 2018-06-22 11:45
 * Description: 一句话说明这个类的作用
 * Since 1.0
 */
@RequestMapping(value = "/admin")
@Controller
public class IndexController {

    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private ShiroService shiroService;

    @RequestMapping(value = "/index.html")
    public String index(){
        logger.info("Welcome to Guanglan's Admin System!");
        Subject currentUser = SecurityUtils.getSubject();
        String userName = currentUser.getSession().getAttribute("currentUser").toString();
        logger.info("当前登录用户是：{}",userName);
        List<SysPermission> perms = shiroService.selectParentMenuByUserName(userName);
        List<Menu> menusList = new ArrayList<>();
        for(SysPermission sysPermission : perms){
            Menu menup = new Menu();
            menup.setMenuName(sysPermission.getMenuName());
            menup.setMenuUrl(sysPermission.getMenuUrl());
            List<SysPermission> list = shiroService.selectChildMenuByPid(sysPermission.getId());
            List<Menu> menus = new ArrayList<>();
           for(SysPermission sp : list){
               Menu menu = new Menu();
               menu.setMenuName(sp.getMenuName());
               menu.setMenuUrl(sp.getMenuUrl());
               menus.add(menu);
           }
            menup.setList(menus);
            menusList.add(menup);
        }
        currentUser.getSession().setAttribute("menusList",menusList);
        return "index/index";
    }
}
